#!/bin/bash

if [ $# -ne 2 ]; then
    echo "Usage: $0 <dir1> <dir2>"
    exit 1
fi

dir1="$1"
dir2="$2"
record_file="record.txt"

if [ ! -d "$dir1" ] || [ ! -d "$dir2" ]; then
    echo "Error type"
    exit 1
fi

# Create or clear the record file
> "$record_file"

find "$dir2" -type f | while read file; do
    file_name=$(basename "$file")
    file_in_dir1="$dir1/$file_name"

    if [ ! -e "$file_in_dir1" ]; then
        cp "$file" "$dir1"
        echo "Copied '$file' to '$dir1'" >> "$record_file"
    elif [ "$file" -nt "$file_in_dir1" ]; then
        cp "$file" "$dir1"
        echo "Updated '$file' in '$dir1'" >> "$record_file"
    fi
done

echo "All changes over"
